<?php
/**
 * Created by PhpStorm.
 * User: 陈阳
 * Date: 2021/1/31
 * Time: 9:26
 */
namespace app\admin\model;
use think\facade\Request;
use think\model;
class Goods extends Model
{
    public function getRecommendedAttr($value){
        if(\think\facade\Config::has('search.recommended')){
            $config = \think\facade\Config::get('search.recommended');
            return $config[$value];
        }
    }

    public function getIfShowAttr($value){
        if(\think\facade\Config::has('search.goods_if_show')){
            $config = \think\facade\Config::get('search.goods_if_show');
            return $config[$value];
        }
    }
    public function getAddTimeAttr($value)
    {
        return date('Y-n-j H:i:s',$value);
    }
    public function searchKeywordsAttr($query,$value){
        if(''!==$value){
            $query->where('goods_name','like','%'.$value.'%');
        }
    }
    public function searchIfShowAttr($query,$value)
    {
        if(is_array($value) && 1 == count($value)) {
            $query->where('g.if_show',$value['0']);
        }

    }
    public function searchRecomAttr($query,$value){
        if(isset($value) && is_array($value)){
            $query->whereIn('ecm_goods.recommended',$value);
        }

        if(isset($value) && is_scalar($value) && '200' != $value){
            $query->where('ecm_goods.recommended',$value);
        }
    }
    public function searchBidAttr($query,$value,$data)
    {
        if(''!==$value){
            $query->where('brand',$value);
        }

    }
    public function searchMaxtimeAttr($query,$value){
        if(!empty($value)){
            $query->whereTime('add_time','>=',$value);
        }
    }
    public function searchMintimeAttr($query,$value){
        if(!empty($value)){
            $query->whereTime('add_time','<=',$value);
        }
    }

    public function searchOrderAttr($query,$value){
        if('desc' == $value){
            $query->order('add_time','desc');
        }elseif('asc' == $value){
            $query->order('add_time');
        }else{
            $query->order('goods_id');
        }
    }
    public function searchCateIdAttr($query,$value){
        if(!empty($value)){
            $query->where("c.cate_id={$value}");
        }
    }
    public function searchMaxJiaAttr($query,$value){
        if(!empty($value)){
            $query->where('price','<=',$value);
        }
    }
    public function searchMinJiaAttr($query,$value){
        if(!empty($value)){
            $query->where('price','>=',$value);
        }
    }
    public static function getGoodsList($data = [],$query = [],$page = true){


        $field = [
            'goods_id','goods_name','price','g.cate_id','add_time','g.recommended','g.if_show','default_image',
            'b.brand_id','b.brand_name','c.cate_id','c.cate_name',

        ];
        $model = self::field($field)->alias('g')
            ->withSearch(array_keys($data),$data)
            ->join(['ecm_brand'=>'b'],'g.brand=b.brand_id','LEFT')
            ->join(['ecm_gcategory'=>'c'],'g.cate_id=c.cate_id','LEFT');
        if($page){
            $list_rows = \think\facade\Config::has('view.list_rows')?\think\facade\Config::get('view.list_rows'):15;
            return $model->paginate([
                'list_rows' => $list_rows,//分页的配置参数
                'query'     => $query,
            ])->each(function($values,$index){
                $values['goods_name']=
                    str_replace(Request::get('keywords'),"<span style='color: red;font-weight: bold;'>".Request::get('keywords')."</span>",$values['goods_name']);
                return $values;
            });
        }else{
            return $model->select();
        }

    }





    public static function getGoodsListt($data = [],$query = [],$page = true){


        $field = [
            'goods_id','goods_name','price','g.cate_id','add_time','g.recommended','g.if_show','default_image',
            'b.brand_id','b.brand_name','c.cate_id','c.cate_name',

        ];

        $model = self::field($field)->alias('g')
            ->join(['ecm_brand'=>'b'],'g.brand=b.brand_id','LEFT')
            ->join(['ecm_gcategory'=>'c'],'g.cate_id=c.cate_id','LEFT');
        if(isset($data['keywords']) && '' !== $data['keywords']){
            $model->whereLike('goods_name','%'.$data['keywords'].'%');
        }
        if(is_array($data['if_show']) && 1 == count($data['if_show'])) {
            $model->where('g.if_show',$data['if_show']['0']);
        }
        if(isset($data['recom']) && is_array($data['recom'])){
            $model->whereIn('g.recommended',$data['recom']);
        }

        if(isset($data['recom']) && is_scalar($data['recom']) && '200' != $data['recom']){
            $model->where('g.recommended',$data['recom']);
        }
        if(isset($data['bid']) && '' !== $data['bid']){
            $model->where('brand',$data['bid']);
        }
        if(!empty($data['maxtime'])){
            $model->whereTime('add_time','>=',$data['maxtime']);
        }
        if(!empty($data['mintime'])){
            $model->whereTime('add_time','<=',$data['mintime']);
        }
        if(!empty($data['cate_id'])){
            $model->where("c.cate_id={$data['cate_id']}");
        }
        if(isset($data['maxjia']) && '' !== $data['maxjia']){
            $model->where('price','<=',$data['maxjia']);
        }
        if(isset($data['minjia']) && '' !== $data['minjia']){
            $model->where('price','>=',$data['minjia']);
        }

        if('desc' == $order){
            $model->order('add_time','desc');
        }elseif('asc' == $order){
            $model->order('add_time');
        }else{
            $model->order('goods_id');
        }
        if($page){
            $list_rows = \think\facade\Config::has('view.list_rows')?\think\facade\Config::get('view.list_rows'):15;
            return $model->paginate([
                'list_rows' => $list_rows,//分页的配置参数
                'query'     => $query,
            ])->each(function($values,$index){
                $values['goods_name']=
                    str_replace(Request::get('keywords'),"<span style='color: red;font-weight: bold;'>".Request::get('keywords')."</span>",$values['goods_name']);
                return $values;
            });
        }else{
            return $model->select();
        }

    }
}